/**
 *  Preloader class
 *
 *  @author : jihuaizhi
 *  @Time   : 2010.03.22 21:08:10 CST
 *  @note   : This class provides a custom preloader in order to delay the initialization
   process of this application for external API's.
 */
package com.basestone.pdlmis.util
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.events.TimerEvent;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.utils.ByteArray;
    import flash.utils.Timer;

    import mx.events.FlexEvent;
    import mx.preloaders.DownloadProgressBar;

    public class Preloader extends DownloadProgressBar
    {
        [Embed(source="../../images/logo2.png", mimeType="application/octet-stream")]
        private var LogoClass:Class;

        private var logo:Loader;
        private var title:TextField;
        private var logText:TextField;

        private var isInitialized:Boolean;
        private var message:String;
        private var preMSG:String;
        private var counter:Number;

        public function Preloader()
        {
            super();
            MINIMUM_DISPLAY_TIME=5000;
            this.addEventListener(Event.COMPLETE, completeEventHandler);

            this.isInitialized=false;
            this.message="SDLMIS Application, Version 1.0\n";
            this.preMSG="";
            this.counter=0;

            this.logo=new Loader();
            this.logo.x=20;
            this.logo.y=15;
            this.logo.visible=false;
            this.addChild(this.logo);
            this.logo.loadBytes(new LogoClass() as ByteArray);

            this.title=new TextField();
            this.title.width=200;
            this.title.height=30;
            this.title.autoSize=TextFieldAutoSize.LEFT;
            this.title.text="SDLMIS Application, Version 1.0\n";
            this.title.visible=false;
            this.addChild(this.title);

            this.logText=new TextField();
            this.logText.x=20;
            this.logText.y=100;
            this.logText.width=300;
            this.logText.height=400;
            this.logText.autoSize=TextFieldAutoSize.LEFT;
            this.logText.text=this.message;
            this.logText.visible=true;
            this.addChild(this.logText);

            var updateChildTimer:Timer=new Timer(100, 1);
            updateChildTimer.addEventListener(TimerEvent.TIMER, updateChild);
            updateChildTimer.start();
        }

        override public function set preloader(preloader:Sprite):void
        {
            super.preloader=preloader;
            preloader.addEventListener(ProgressEvent.PROGRESS, downloadInProgressEventHandler);
            preloader.addEventListener(Event.COMPLETE, downloadCompletedEventHandler);
            preloader.addEventListener(FlexEvent.INIT_PROGRESS, initInProgressEventHandler);
            preloader.addEventListener(FlexEvent.INIT_COMPLETE, initCompletedEventHandler);
        }

        override protected function showDisplayForInit(elapsedTime:int, count:int):Boolean
        {
            return true;
        }

        override protected function showDisplayForDownloading(elapsedTime:int, event:ProgressEvent):Boolean
        {
            return true;
        }

        private function updateMessage(option:Number, msg:String):void
        {
            var tmpMsg1:String;
            var tmpMsg2:String;

            if (logText == null)
            {
                return;
            }

            switch (option)
            {
                case 0:
                case 1:
                    logText.appendText(msg);
                    preMSG="";
                    break;
                case 2:
                    logText.appendText("\n" + msg);
                    preMSG=msg;
                    break;
                case 3:
                    logText.appendText(msg);
                    preMSG=msg;
                    break;
                default:
                    break;
            }
        }

        private function completeEventHandler(event:Event):void
        {
            if (!isInitialized)
            {
                event.stopImmediatePropagation();
                setProgress(100, 100);
            }
        }

        private function updateChild(event:Event):void
        {
            this.logo.visible=false;
            this.title.x=this.stageWidth / 2 - this.title.width / 2;
            this.title.y=this.stageHeight / 2 - this.title.height / 2 - 50;
            this.title.visible=true;
        }

        private function downloadInProgressEventHandler(event:ProgressEvent):void
        {
            this.updateMessage(0, "Downloading: " + event.bytesLoaded + " of " + event.bytesTotal);
        }

        private function downloadCompletedEventHandler(event:Event):void
        {
            this.updateMessage(1, "\nDownloading the application is completed!");
        }

        private function initInProgressEventHandler(event:Event):void
        {
            var dots:String="";
            if (counter < 10)
            {
                counter++;
                for (var i:Number=0; i < counter; i++)
                {
                    dots=dots + ".";
                }
            }
            else
            {
                counter == 0;
                dots=".";
            }

            this.updateMessage(0, "Initializing" + dots);
        }

        private function initCompletedEventHandler(event:Event):void
        {
            this.updateMessage(3, "\nInitializating the application is completed!");
            var timer:Timer=new Timer(10, 1);
            timer.addEventListener(TimerEvent.TIMER, dispatchComplete);
            timer.start();
        }

        private function dispatchComplete(event:TimerEvent):void
        {
            isInitialized=true;
            this.dispatchEvent(new Event(Event.COMPLETE, false, true));
        }
    }
}